﻿using System;
using System.Collections.Generic;
using System.Text;
using MCromwell.StaffIntranet.Core;
using MCromwell.StaffIntranet.DependencyInjection;
using MCromwell.StaffIntranet.DTO;
using MCromwell.StaffIntranet.Task.Infrastructure;
using Mapper;

namespace MCromwell.StaffIntranet.Task.Tasks
{
    public class CommonTask : ICommonTask
    {
        private IAdministrationRepository adminRepository;
        public CommonTask(IAdministrationRepository adminRepository)
        {
            if (adminRepository == null)
                throw new ArgumentNullException("adminRepository", "adminRepository cannot be null");

            this.adminRepository = adminRepository;
        }

        public CommonTask()
            : this(IoC.Resolve<IAdministrationRepository>())
        {
        }

        [WrapExceptionWith(typeof(ProblemtRetrievingAdministrator))]
        public AdministratorDTO RetrieveAdministratorFor(Guid Session)
        {
            LoginSession foundSession = adminRepository.FindLoginSessionBy(Session);
            if (foundSession != null)
                return MapToDTOFrom(foundSession.Administrator);

            return null;
        }

        private static AdministratorDTO MapToDTOFrom(Administrator administrator)
        {
            var mapper = SimpleObjectMapperContainer.RetrieveMapper<Administrator, AdministratorDTO>();
            return mapper.Apply(administrator);
        }

    }
}
